lpsolve是sourceforge下的一个开源项目,它的介绍如下:
Mixed Integer Linear Programming (MILP) solver lp_solve solves pure linear, (mixed) integer/binary, semi-cont and special ordered sets (SOS) models.lp_solve is written in ANSI C and can be compiled on many different platforms like Linux and WINDOWS .
lpsolve是一个混合整数线性规划求解器,可以求解纯线性、(混合)整数/二值、半连续和特殊有序集模型。并且经过实际验证,有极高的求解效率。
1.在Windows x64和Matlab环境下使用lpsolve
需要在网址lpsolve_5.5.2.5 提供的文件列表中下载文件lp_solve_5.5.2.5_MATLAB_exe_win64.zip。或者下载文件lp_solve_5.5.2.5_source.tar.gz自行编译dll。
注:在Matlab下运行示例报错:
Error using mxlpsolve
Failed to initialise lpsolve library.
参考Using lpsolve from MATLAB: http://web.mit.edu/lpsolve/doc/MATLAB.htm给出的说明,需要将编译得到的mxlpsolve.dll拷贝到 WINDOWS\system32文件夹下。
2.在MacOS下使用lpsolve
参考Using lp_solve in Java with Mac OS X的配置说明,下载源文件lp_solve_5.5.2.5_source.tar.gz,然后跳转到lp_solve_5.5/lpsolve55文件夹内,并执行ccc.osx:
cd lp_solve_5.5/lpsolve55
sh ccc.osx
生成的文件所在的文件夹:lpsolve55/bin/osx64/,将此文件夹内生成的两个文件liblpsolve55.dylib,liblpsolve55.a拷贝到/usr/local/lib文件夹内即可:
sudo cp liblpsolve55.a liblpsolve55.dylib /usr/local/lib
测试demo:
cd lp_solve_5.5/demo
sh ccc
./demo
3.在MacOS的Matlab中需要文件mxlpsolve.mexmaci64
需要从lpsolve主页下载源文件lp_solve_5.5.2.5_MATLAB_source.tar.gz,解压缩后,在Matlab中执行文件Makefile.m,期间需要添加各种头文件:
lp_Hash.h
lp_lib.h
lp_matrix.h
lp_mipbb.h
lp_SOS.h
lp_types.h
lp_utils.h
可下载lp_solve_5.5.2.5_dev_ux64.tar.gz并从中获取即可。
4.举例
mxlpsove.m是建模的核心函数,一个线性规划模型的所有配置和求解都是通过这个函数完成的。lp_maker.m和lp_solve.m是对mxlpsolve.m的高层包装,简化了模型建立和求解的过程。例如用lpsolve求解数学规划问题:
$$ \max 4x_1+2x_2+x_3\\
s.t.~ 2x_1+x_2\le 1\\
x_1+2x_3\le 2\\
x_1+x_2+x_3=1\\
0\le x_1\le1\\
0\le x_2\le1\\
0\le x_3\le2$$
相应的Matlab语句为:
f = [4 2 1];
A = [2 1 0; 1 0 2; 1 1 1];
b = [1; 2; 1];
l = [ 0 0 0];
u = [ 1 1 2];
lp=mxlpsolve('make_lp', 1, 3);
mxlpsolve('set_verbose', lp, 3);
mxlpsolve('set_obj_fn', lp, f);
mxlpsolve('add_constraint', lp, A(1, :), 1, b(1));
mxlpsolve('add_constraint', lp, A(2, :), 1, b(2));
mxlpsolve('add_constraint', lp, A(3, :), 0, b(3);
mxlpsolve('set_lowbo', lp, l);
mxlpsolve('set_upbo', lp, u);
mxlpsolve('write_lp', lp, 'a.lp');
mxlpsolve('get_mat', lp, 1, 2)
mxlpsolve('solve', lp)
mxlpsolve('get_objective', lp)
mxlpsolve('get_variables', lp)
mxlpsolve('get_constraints', lp)
mxlpsolve('delete_lp', lp)
重要函数说明:
lp_solve
LP_SOLVE Solves mixed integer linear programming problems.
SYNOPSIS: [obj,x,duals] = lp_solve(f,a,b,e,vlb,vub,xint,scalemode,keep)
solves the MILP problem
max v = f'*x
a*x <> b
vlb <= x <= vub
x(int) are integer
ARGUMENTS: The first four arguments are required:
f: n vector of coefficients for a linear objective function.
a: m by n matrix representing linear constraints.
b: m vector of right sides for the inequality constraints.
e: m vector that determines the sense of the inequalities:
e(i) = -1 ==> Less Than
e(i) = 0 ==> Equals
e(i) = 1 ==> Greater Than
vlb: n vector of lower bounds. If empty or omitted,
then the lower bounds are set to zero.
vub: n vector of upper bounds. May be omitted or empty.
xint: vector of integer variables. May be omitted or empty.
scalemode: scale flag. Off when 0 or omitted.
keep: Flag for keeping the lp problem after it's been solved.
If omitted, the lp will be deleted when solved.
OUTPUT: A nonempty output is returned if a solution is found:
obj: Optimal value of the objective function.
x: Optimal value of the decision variables.
duals: solution of the dual problem.
Example of usage. To create and solve following lp-model:
max: -x1 + 2 x2;
C1: 2x1 + x2 < 5;
-4 x1 + 4 x2 <5;
int x2,x1;
The following command can be used:
>> [obj, x]=lp_solve([-1, 2], [2, 1; -4, 4], [5, 5], [-1, -1], [], [], [1, 2])
obj =
3
x =
1
2
lp_maker
LP_MAKER Makes mixed integer linear programming problems.
SYNOPSIS: lp_handle = lp_maker(f,a,b,e,vlb,vub,xint,scalemode,setminim)
make the MILP problem
max v = f'*x
a*x <> b
vlb <= x <= vub
x(int) are integer
ARGUMENTS: The first four arguments are required:
f: n vector of coefficients for a linear objective function.
a: m by n matrix representing linear constraints.
b: m vector of right sides for the inequality constraints.
e: m vector that determines the sense of the inequalities:
e(i) < 0 ==> Less Than
e(i) = 0 ==> Equals
e(i) > 0 ==> Greater Than
vlb: n vector of non-negative lower bounds. If empty or omitted,
then the lower bounds are set to zero.
vub: n vector of upper bounds. May be omitted or empty.
xint: vector of integer variables. May be omitted or empty.
scalemode: Autoscale flag. Off when 0 or omitted.
setminim: Set maximum lp when this flag equals 0 or omitted.
OUTPUT: lp_handle is an integer handle to the lp created.
Example of usage. To create following lp-model:
max: -x1 + 2 x2;
C1: 2x1 + x2 < 5;
-4 x1 + 4 x2 <5;
int x2,x1;
The following command can be used:
>> lp=lp_maker([-1, 2], [2, 1; -4, 4], [5, 5], [-1, -1], [], [], [1, 2])
lp =
0
To solve the model and get the solution:
>> mxlpsolve('solve', lp)
ans =
0
>> mxlpsolve('get_objective', lp)
ans =
3
>> mxlpsolve('get_variables', lp)
ans =
1
2
注意:Don’t forget to free the handle and its associated memory when you are done:
>> mxlpsolve('delete_lp', lp);
5.参考
https://diegoresearch.wordpress.com/2008/07/10/using-lp_solve-in-java-with-mac-os-x/